//
//  NewsTotalVC.m
//  sensen_ios
//
//  Created by mc on 16/3/23.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "NewsTotalVC.h"

#import "HMSegmentedControl.h"
#import "ZhangSegmentView.h"

#import "NewsMoreVC.h"

@interface NewsTotalVC (){
    NSArray * aryTitle;  //订单标题
   
}

@property (strong, nonatomic)  ZhangSegmentView *segTitle;
@property (strong, nonatomic)  UIScrollView *svContent;




@end

@implementation NewsTotalVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"资讯热读";
    // 创建导航栏左边返回按钮
    [self createLeftBarButtonItemWithImagename:nil];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self _initViews];
    [self _initData];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    // 设置导航栏颜色
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:SS_NAVI_ORANGE_COLOR] forBarMetrics:UIBarMetricsDefault];
    // 隐藏标签栏
    MainTabBarController *main = (MainTabBarController *)self.navigationController.tabBarController;
    [main hideTabBar];
}


#pragma mark -- 创建视图 --
- (void)_initViews{
    self.svContent = [[UIScrollView alloc] init];
    [self.view addSubview:self.svContent];
    [self.svContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(0);
        make.left.equalTo(self.view).offset(0);
        make.top.equalTo(self.view).offset(36);
        make.bottom.equalTo(self.view).offset(0);
        
    }];
    
}

#pragma mark -- 初始化数据 --
- (void)_initData{
    aryTitle = @[@"时政要闻",@"森森夜话",@"休闲娱乐",@"宠物世界",@"周末走呗",@"心灵微光"];
    __weak typeof(self) weakSelf = self;
    self.segTitle = [[ZhangSegmentView alloc] initWithFrame:CGRectMake(0, self.view.top, SCREEN_WIDTH, 36) titles:aryTitle clickBlick:^(NSInteger index) {
        [weakSelf changeSelect:index - 1 animated:NO];
    }];
    self.segTitle.titleNomalColor = [BoyeTool colorFromHexRGB:@"999999"];
    [self.view addSubview:self.segTitle];
    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 35, SCREEN_WIDTH, 1)];
    lbl.backgroundColor = [BoyeTool colorFromHexRGB:@"dbdbdb"];
    
    [self.view addSubview:self.segTitle];
    [self.segTitle addSubview:lbl];
    
    self.svContent.pagingEnabled = YES;
    self.svContent.showsHorizontalScrollIndicator = NO;
    self.svContent.contentSize = CGSizeMake(SCREEN_WIDTH*aryTitle.count, 200);
    self.svContent.delegate = self;
    [self performSelector:@selector(delaySelect) withObject:nil afterDelay:0.1];
}

- (void)delaySelect{
    [self changeSelect:0 animated:NO];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat pageWidth = scrollView.frame.size.width;
    NSInteger page = scrollView.contentOffset.x / pageWidth;
    [self.segTitle setSelectedSegmentIndex:page + 1 animated:YES];
    [self addViewController:page];
}

- (void)changeSelect:(NSInteger)intSelect animated:(BOOL)animated{
    [self.segTitle setSelectedSegmentIndex:intSelect + 1 animated:animated];
    [UIView animateWithDuration:0.2 animations:^{
        [self.svContent setContentOffset:CGPointMake(SCREEN_WIDTH*intSelect, 0) animated:animated];
    }];
    [self addViewController:intSelect];
    
}

- (void)addViewController:(NSInteger)index{
    NSMutableArray *maryIndex = [[NSMutableArray alloc]init];
    for (UIViewController *vc in self.childViewControllers) {
        if ([vc isKindOfClass:[NewsMoreVC class]]) {
            //每次进入都重新下载View
            [maryIndex addObject:[NSNumber numberWithInteger:vc.view.tag]];
            
        }
    }
    
    if (![maryIndex containsObject:[NSNumber numberWithInteger:index]]) {
        NewsMoreVC *vc =  [[NewsMoreVC  alloc] init];
        vc.type = index;
        [self addChildViewController:vc];
        vc.view.frame = CGRectMake(SCREEN_WIDTH*index, 0, self.svContent.frame.size.width, self.svContent.frame.size.height);
        [self.svContent addSubview:vc.view];
        vc.view.tag = index;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
